package com.waqu.common.utils.datetime;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.waqu.common.utils.text.StringTools;

public class TimeTools {

	private static final SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

	/** 取得当前时间戳与modulus的模数 */
	public static long getTimestampModulus(int modulus) {
		return getCurrentTime() % modulus;
	}

	/** 让线程休眠一段时间 */
	public static void sleep(int seconds) {
		sleepMillis(seconds * 1000);
	}

	/** 休眠N毫秒 */
	public static void sleepMillis(long millis) {
		try {
			Thread.sleep(millis);
		} catch (Exception ex) {
		}
	}

	/** 根据一个Unix时间截取得一个日期字符串（yyyy-MM-dd HH:mm:ss） */
	public static String getDateTime(long timestamp) {
		return dateTimeFormat.format(new Date(timestamp));
	}

	public static String getDateTime(long timestamp, String format) {
		return new SimpleDateFormat(format).format(new Date(timestamp));
	}

	public static String getDateTime(long timestamp, SimpleDateFormat format) {
		return format.format(new Date(timestamp));
	}

	/**
	 * 根据一个日期字符串取得一个Unix时间截
	 * @param dateStr 如:2009-05-15,或2009-05-15 00:00:01
	 */
	public static long getUnixTime(String dateStr) {
		Date date = null;
		try {
			date = dateTimeFormat.parse(dateStr.trim());
		} catch (ParseException ex) {
			try {
				date = dateFormat.parse(dateStr.trim());
			} catch (ParseException ex2) {
			}
		}

		return date != null ? date.getTime() : 0L;
	}

	/** 取得当前的时间戳 */
	public static long getCurrentTime() {
		return System.currentTimeMillis();
	}

	/** 取得当前的时间戳（秒） */
	public static long getCurrentTimeSeconds() {
		return System.currentTimeMillis() / 1000;
	}

	/** 取得当前的日期 */
	public static String getCurrentDate() {
		return getDateTime(System.currentTimeMillis());
	}

	/** 将时间秒数格式化hh:mm:ss的格式 */
	public static String format(long seconds) {
		int h = (int) (seconds / 3600);
		int m = (int) (seconds - h * 3600) / 60;
		int s = (int) (seconds - h * 3600 - m * 60);
		return String.format("%s:%s:%s", h, m, s);
	}
	
	private static String transferDouble(int in) {
		return in > 9 ? in + "" : "0" + in;
	}
	
	/** 将时间秒数格式化hh:mm:ss的格式 */
	public static String doubleFormat(long seconds) {
		int h = (int) (seconds / 3600);
		int m = (int) (seconds - h * 3600) / 60;
		int s = (int) (seconds - h * 3600 - m * 60);
		return String.format("%s:%s:%s", transferDouble(h), transferDouble(m), transferDouble(s));
	}
	
	/** 获取当前周一的开始时间 */
	public static long getWeekStartTime() {
		Calendar c = Calendar.getInstance();
		c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
		c.set(Calendar.MILLISECOND, 0);
		int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
		if (day_of_week == 0) // 星期天
			day_of_week = 7;
		c.add(Calendar.DATE, -day_of_week + 1);
		return c.getTimeInMillis();
	}
	
	/** 获取当前天是星期几 */
	public static int getCurrentDayInWeek() {
		Calendar c = Calendar.getInstance();
		int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
		return day_of_week == 0 ? 7 : day_of_week; // 星期天
	}
	
	/** 获取当天的开始时间 */
	public static long getCurrentDayFirstTime() {
		Calendar c = Calendar.getInstance();
		c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
		c.set(Calendar.MILLISECOND, 0);
		return c.getTimeInMillis();
	}
	
	/** 获取当天的结束时间 */
	public static long getCurrentDayLastTime() {
		Calendar c = Calendar.getInstance();
		c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH) + 1, 0, 0, 0);
		c.set(Calendar.MILLISECOND, 0);
		c.add(Calendar.MILLISECOND, -1);
		return c.getTimeInMillis();
	}
	
	/** 获取当前月任意一天的开始时间 */
	public static long getCurrentMonthAnyDayFirstTime(int anyDay) {
		if (anyDay < 1 || anyDay > 31)
			throw new RuntimeException("anyDay values [1~31] error anyDay value : " + anyDay);
		Calendar c = Calendar.getInstance();
		c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), anyDay, 0, 0, 0);
		c.set(Calendar.MILLISECOND, 0);
		return c.getTimeInMillis();
	}
	
	/** 获取当前月任意一天的结束时间 */
	public static long getCurrentMonthAnyDayLastTime(int anyDay) {
		if (anyDay < 1 || anyDay > 31)
			throw new RuntimeException("anyDay values [1~31] error anyDay value : " + anyDay);
		Calendar c = Calendar.getInstance();
		c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), anyDay + 1, 0, 0, 0);
		c.set(Calendar.MILLISECOND, 0);
		c.add(Calendar.MILLISECOND, -1);
		return c.getTimeInMillis();
	}
	
	/** 获取当前月开始的时间 */
	public static long getCurrentMonthFirstTime() {
		Calendar c = Calendar.getInstance();
		c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), 1, 0, 0, 0);
		c.set(Calendar.MILLISECOND, 0);
		return c.getTimeInMillis();
	}
	
	/** 获取当前月最后一刻的时间 */
	public static long getCurrentMonthLastTime() {
		Calendar c = Calendar.getInstance();
		c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH) + 1, 1, 0, 0, 0);
		c.set(Calendar.MILLISECOND, 0);
		c.add(Calendar.MILLISECOND, -1);
		return c.getTimeInMillis();
	}
	
	/** 获取当前周任意一天的开始时间 */
	public static long getCurrentWeekAnyDayStartTime(int anyDay) {
		if (anyDay < 1 || anyDay > 7)
			throw new RuntimeException("anyDay values [1~7] error anyDay value : " + anyDay);
		Calendar c = Calendar.getInstance();
		c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
		c.set(Calendar.MILLISECOND, 0);
		int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
		if (day_of_week == 0) // 星期天
			day_of_week = 7;
		c.add(Calendar.DAY_OF_MONTH, -day_of_week + anyDay);
		return c.getTimeInMillis();
	}
	
	/** 获取当前周任意一天的结束时间 */
	public static long getCurrentWeekAnyDayLastTime(int anyDay) {
		if (anyDay < 1 || anyDay > 7)
			throw new RuntimeException("anyDay values [1~7] error anyDay value : " + anyDay);
		Calendar c = Calendar.getInstance();
		c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH) + 1, 0, 0, 0);
		c.set(Calendar.MILLISECOND, -1);
		int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
		if (day_of_week == 0) // 星期天
			day_of_week = 7;
		c.add(Calendar.DAY_OF_MONTH, -day_of_week + anyDay);
		return c.getTimeInMillis();
	}
	
	/** 获取今天所属全年第几周<br/>如：201701 */
	public static int getCurrentWeekOfYear() {
		Calendar calendar = Calendar.getInstance();
		int week = calendar.get(Calendar.WEEK_OF_YEAR);
		if (calendar.get(Calendar.DAY_OF_WEEK) - 1 == 0) // 星期天
			week--;
		if(week == 0) { // 新年
			calendar.add(Calendar.DAY_OF_MONTH, -1);
			week = calendar.get(Calendar.WEEK_OF_YEAR);
		}
		return Integer.parseInt(calendar.get(Calendar.YEAR) + StringTools.twoDigits(week));
	}
	
}